<template>
  <div class='setting-container'>
    <el-card  v-loading="loading">
      <!-- 导航 -->
      <div slot="header">
        <Breadcrumb>个人设置</Breadcrumb>
      </div>

      <!-- 栅格 -->
      <el-row>
        <el-col :span="12">
          <!-- 表单 -->
          <el-form label-width="120px"  :rules="rules">
            <el-form-item label="编号：">100</el-form-item>
            <el-form-item label="手机：">{{userInfo.mobile}}</el-form-item>
            <el-form-item label="媒体名称：" prop="name">
              <el-input v-model="userInfo.name"></el-input>
            </el-form-item>
            <el-form-item label="媒体简介：" prop="intro">
              <el-input v-model="userInfo.intro" type="textarea" :rows="3"></el-input>
            </el-form-item>
            <el-form-item label="联系邮箱：">
              <el-input v-model="userInfo.email"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="hUpdate" type="primary">保存设置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <!-- 上传组件 action必须属性-->
          <el-upload
            class="avatar-uploader"
            action=""
            :http-request="uploadPhoto"
            :show-file-list="false">
            <img v-if="userInfo.photo" :src="userInfo.photo" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="edit-photo">修改头像</p>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
// import auth from '@/utils/auth'
export default {
  name: 'my-setting',
  data () {
    return {
      loading: false,
      rules: {
        // 手机验证
        // trigger
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        intro: [
          { required: true, message: '请输入简介', trigger: 'blur' }
        ]
      },
      // 用户信息
      userInfo: {
        id: '',
        name: '',
        intro: '',
        email: '',
        mobile: '',
        photo: ''
      },
      // 支持上传组件的数据
      imageUrl: null
    }
  },
  created () {
    this.loadInfo()
  },
  methods: {
    // 调用接口并初始化数据
    async loadInfo () {
      const res = await this.$http({
        method: 'GET',
        url: '/mp/v1_0/user/profile'
      })
      console.log('大师傅士大夫', res)
      this.userInfo.name = res.data.data.name
      this.userInfo.email = res.data.data.email
      this.userInfo.intro = res.data.data.intro
      this.userInfo.mobile = res.data.data.mobile
      this.userInfo.photo = res.data.data.photo
      this.loading = false
    },
    // 修改姓名
    async hUpdate () {
      this.loading = true
      try {
        const res = await this.$http({
          method: 'PATCH',
          url: '/mp/v1_0/user/profile',
          data: {
            name: this.userInfo.name,
            intro: this.userInfo.intro,
            email: this.userInfo.email
          }
        })
        this.loading = false
        console.log('11111111111111', res)
        this.$eventBus.$emit('update_user_Bokname', this.userInfo.name)
        // 同步头像到本地
        // const localUser = auth.getUser()
        // localUser.name = this.userInfo.name
        // auth.setUser(localUser)
        this.$message.success('修改姓名成功')
      } catch (err) {
        console.log(err)
        this.loading = false
        this.$message.error('修改姓名失败11111111')
      }
    },
    // 上传图片
    async uploadPhoto (obj) {
      this.loading = true
      // 1.获取本次要上传的数据
      const { file } = obj
      // console.log(file)
      // 2.发送请求更新窗口
      const fd = new FormData()
      // 这个接口本质做的是文件上传，所以传参时，不能用普通的对象格式。
      // 而应该使用formData
      fd.append('photo', file)
      try {
        const res = await this.$http({
          method: 'PATCH',
          url: '/mp/v1_0/user/photo',
          data: fd
        })
        this.loading = false
        console.log('1111111111111111111sss', res)
        this.userInfo.photo = res.data.data.photo
        // 通过事件总线来传值
        this.$eventBus.$emit('update_user_photo', res.data.data.photo)
        this.$message.success('修改图片成功')
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    }

  }
}
</script>
<style lang="less" scoped>
.text {
  font-size: 12px;
  text-align: center;
}
.avatar{
    width: 178px;
    height: 178px;
    line-height: 178px;
  }
  .avatar-uploader {
      text-align: center;
  }
  .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
  }
  .avatar-uploader /deep/ .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
  }
.edit-photo{
  font-size: 12px;
  text-align: center;
}
</style>
